package com.example.shop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.shop.pojo.EvaluationUserThumbUp;
import com.example.shop.pojo.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {

    @Select("select * from goods where goods_id=#{goodsId}")
    Goods selectById(@Param("goodsId") Long goodsId);

    @Select("select * from goods where merchant_id=#{merchantId}")
    List<Goods> selectByMerchantId(@Param("merchantId")Long merchantId);

    @Select("select merchant_id from goods where goods_id = ${goodsId}")
    Long selectMerchantIdById(Long goodsId);

    @Select("select merchant_account from goods g join merchant m on g.merchant_id = m.merchant_id  where goods_id = ${goodsId}")
    String selectAccountById(Long goodsId);

    /**
     * 对该商品的好评数进行更改，并对应更新该商品的分值
     * @param goodsId 商品id
     * @param num 要在原基础上的变更数
     * @return 返回更新行数
     */
    @Update("update goods " +
            "set " +
            "favorable_comment_number = favorable_comment_number + ${num}," +
            "recommend_score = recommend_score + ${num * 100} " +
            "where goods_id = ${goodsId}")
    int updateFavorableCommentNumber(@Param("goodsId") long goodsId,@Param("num") int num);

    /**
     * 对该商品的中评数进行更改，并对应更新该商品的分值
     * @param goodsId 商品id
     * @param num 要在原基础上的变更数
     * @return 返回更新行数
     */
    @Update("update goods set medium_comment_number = medium_comment_number + ${num} where goods_id = ${goodsId}")
    int updateMediumCommentNumber(@Param("goodsId")long goodsId,@Param("num")int num);

    /**
     * 对该商品的差评数进行更改，并对应更新该商品的分值
     * @param goodsId 商品id
     * @param num 要在原基础上的变更数
     * @return 返回更新行数
     */
    @Update("update goods " +
            "set " +
            "bad_comment_number = bad_comment_number + ${num}," +
            "recommend_score = recommend_score - ${num * 100} " +
            "where goods_id = ${goodsId}")
    int updateBadCommentNumber(@Param("goodsId")long goodsId,@Param("num")int num);

    /**
     * 对该商品的销售数进行更改
     * @param goodsId 商品id
     * @param num 要在原基础上的变更数
     * @return 返回更新行数
     */
    @Update("update goods set purchased_time = purchased_time + ${num} where goods_id = ${goodsId}")
    int updatePurchasedTime(@Param("goodsId")long goodsId,@Param("num")int num);



    @Delete("DELETE goods,r_goods_class,r_goods_description_picture,r_goods_details_picture " +
            "from " +
            "goods " +
            "left JOIN r_goods_class " +
            "on goods.goods_id = r_goods_class.goods_id " +
            "left JOIN r_goods_description_picture " +
            "on goods.goods_id = r_goods_description_picture.goods_id " +
            "LEFT JOIN r_goods_details_picture " +
            "on goods.goods_id = r_goods_details_picture.goods_id " +
            "where goods.goods_id =  ${goodsId}")
    int deleteAllInfoAboutGoods(long goodsId);
}
